package com.mm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.entity.MmLog;
import com.mm.exception.BusinessException;
import com.mm.service.impl.MmLogServiceImpl;
import com.mm.plugin.logging.enums.ResultCodeEnum;
import com.mm.utils.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
* <p>
* 日志表 前端控制器
* </p>
*
* @author wdx
* @since 2021-03-04
*/
@Api(tags = "日志API")
@Validated
@RestController
@RequestMapping("/mm-log")
public class MmLogController {

    @Autowired
    MmLogServiceImpl mmLogService;

    @ApiOperation(value = "分页列表")
    @GetMapping("/{pageNo}/{pageSize}")
    @ApiImplicitParams(value = {
    @ApiImplicitParam(name = "pageNo", value = "当前页", defaultValue = "1", dataType = "int", paramType = "query"),
    @ApiImplicitParam(name = "pageSize", value = "一页条数", defaultValue = "10", dataType = "int", paramType = "query")
    })
    public ApiResult<MmLog> page(@NotNull(message = "当前页不能为空") @PathVariable("pageNo") Integer pageNo, @NotNull(message = "页条数不能为空") @PathVariable("pageSize") Integer pageSize) {
        IPage<MmLog> page = mmLogService.page(new Page<>(pageNo, pageSize));
        return ApiResult.success(page);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public ApiResult save(@RequestBody MmLog mLog) {
        boolean save = mmLogService.save(mLog);
        if (!save){
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "详情")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    public ApiResult<MmLog> detail(@NotNull(message = "ID不能为空") @PathVariable Integer id) {
        MmLog mLog = mmLogService.getById(id);
        if (mLog == null) {
            throw new BusinessException(ResultCodeEnum.NO_DATA);
        }
       return ApiResult.success(mLog);
    }


    @ApiOperation(value = "修改")
    @PutMapping
    public ApiResult update(@RequestBody MmLog mLog) {
        boolean update = mmLogService.updateById(mLog);
        if (!update) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    @DeleteMapping("/{id}")
    public ApiResult del(@NotNull(message = "ID不能为空") @PathVariable Integer id) {
        boolean del = mmLogService.removeById(id);
        if (!del) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    /**
    * 查询条件简化
    * @return
    */
    public LambdaQueryWrapper<MmLog> query(){
        return new LambdaQueryWrapper<MmLog>();
    }

}
